package com.woniuxy.reader.controller;



import com.woniuxy.reader.entity.Author;
import com.woniuxy.reader.service.AuthorService;
import com.woniuxy.result.ResponseResult;
import com.woniuxy.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zh_o
 * @since 2020-10-09
 */
@RestController
@RequestMapping("/author")
@Api(tags = "作者接口")
public class AuthorController {

    @Autowired
    private AuthorService authorService;

    /**
     * 作者登录
     * @param tel
     * @param password
     * @return
     */
    @RequestMapping("login")
    @ApiOperation("作者登录")
    public Result login(String tel, String password) {
        return Result.success(authorService.login(tel,password));
    }

    /**
     * 笔名是否重复
     * @param penName
     * @return
     */
    @GetMapping("/register/{penName}")
    public boolean selectByPenName(@PathVariable("penName") String penName){
        return authorService.SelectByPenName(penName);
    }

     /**
     　* @Author daxigua
       * @Description  通过传入作者id，查询作品进行判断，是不是签约作者
       * @Date 2020/10/15  20:11
       * @Param  * @param null
       * @return
       */

    @GetMapping("apply/{authorId}")
    public ResponseResult chickAuthorByid(@PathVariable("authorId") int authorId ){
        boolean b = authorService.selectBookByAuthorIdAndCountAndStatus(authorId);
        return ResponseResult.success(b);
    }

    /**
     * 根据ID获取作者信息
     * @return
     */
    @GetMapping("{authorId}")
    @ApiOperation("根据用户ID查询用户信息")
    @ApiImplicitParam(name = "authorId", value = "作者ID")
    public Object getUser(@PathVariable("authorId") Integer authorId){
        return ResponseResult.success(authorService.getById(authorId));
    }
    /**
     * 作者修改自己的信息
     */
    @PostMapping
    public Object getUserById(@RequestBody Author author){
        return ResponseResult.success(authorService.updateById(author));
    }

    /**
     * 注册成为作者
     * @param author
     * @param userId
     * @return
     */
    @PostMapping("authorRegister")
    public Object insertAuthor(Author author,Integer userId){
        System.out.println(author.toString());
        return  ResponseResult.success(authorService.insertAuthor(author,userId));
    }
    /**
     * 作者申请签约
     */
    @PutMapping("applyConfirm/{authorId}")
    public Object applyAuthor(@PathVariable("authorId") Integer authorId){
        Author author=authorService.getById(authorId);
        author.setLevel("申请签约");
        return authorService.updateById(author);
    }
}

